<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/logo.jpg" alt="" > 
                BBIN电商后台管理
            </div>
            <el-button type="info" @click="loginOut">退出</el-button> 
        </el-header>
        <el-container>
            <!-- 侧边栏区域 -->
            <el-aside width="200px">
                <!-- 折叠功能 -->
                <div class="toggle-buttn" @click="toggleCollapse">|||</div>
                <el-menu
                    background-color="#333744"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    unique-opened
                    :collapse="isCollapse"
                    :collapse-transition="false"
                    router
                    :default-active='activePath'
                    >
                     <!--router 开启路由功能-->
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <!-- 一级菜单的模板区 -->
                        <template slot="title">
                        <!-- 图标 -->
                        <i class="el-icon-circle-plus-outline"></i>
                        <!-- 文本 -->
                        <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item 
                        :index="'/'+subItem.path" 
                        v-for="subItem in item.children" 
                        :key="subItem.id"
                        @click="saveNavState('/'+subItem.path)"
                        >
                            <!-- 二级菜单的模板区 -->
                            <template slot="title">
                            <!-- 图标 -->
                            <i class="el-icon-s-custom"></i>
                            <!-- 文本 -->
                            <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 主体区域 -->
            <el-main>
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        name : 'Home',
        data() {
            return {
                // 左侧菜单数据 , 通过axios请求 把获取到的数据立即挂载到data中
                menuList:[],
                // 是否折叠菜单
                isCollapse : false,
                // 被激活的链接地址
                activePath:'',
            }
        },
        created () {  //生命周期函数,一加载时就要发送请求获取
            this.getMenuList(),  //立即执行获取左侧菜单权限
            this.activePath = window.sessionStorage.getItem('activePath') //从sessionStorage中取出
        },
        methods : {
            loginOut(){  //退出功能
                window.sessionStorage.clear() //清除当地的sessionStorage 也就是 token
                this.$router.push('/login') // 编程式导航到登陆页面
            },
            async getMenuList(){
                const {data : res } = await this.$http.get('menus')
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.menuList = res.data
            },
            toggleCollapse(){  //点击折叠菜单
                this.isCollapse = !this.isCollapse
            },
            saveNavState(activePath){   // 被激活的链接地址
                window.sessionStorage.setItem('activePath',activePath)  //将此地址保存到sessionStorage
                this.activePath = activePath //改变当前的高亮状态
            }
        }
    }
</script>

<style>
.home-container {
  height: 100%;
}
.el-header {
  background-color: rgb(187, 164, 164);
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.el-header div{
    display: flex;
    align-items: center;
}
.el-header div img{
    width:50px;
    margin: 0 20px;
}
.el-aside {
  background-color: #bfa;
  color: #333;
  text-align: center;
  line-height: 200px;
}
.el-aside .el-menu{
    border-right: none;   /*防止子菜单没有对齐 */
}

.toggle-buttn{
    background-color: rgb(172, 117, 117);
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;

}
</style>